package com.inspur.attendance.Dao;

import com.inspur.attendance.Entity.Department;
import com.inspur.attendance.Entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Repository
public interface UserDao extends JpaRepository<User, Long> {
    /**
     * 验证用户是否存在
     * @param account
     * @return
     */
    User findByAccount(String account);

    /**
     * 查询用户账号密码
     * @param account
     * @param password
     * @return
     */
    User findByAccountAndPassword(String account, String password);


    void deleteByAccount(String account) throws Exception;


    List<User> findUsersByNameLike(String name);

    User findTopByName(String account);

    User findTopByAccount(String account);


    @Query(nativeQuery = true, value = "select * from department")
    List<Department> getDepartmentId();

    User findTopById(Long id);

    @Transactional
    @Modifying
    @Query(nativeQuery = true, value = "update user set department_id = '' where department_id = ?1")
    void delDepartmentName(String id);
}
